#include "liveslavewidget.h"
#include "ui_liveslavewidget.h"
#include "mainwidget.h"
#include <QTimer>

LiveSlaveWidget::LiveSlaveWidget(TcpSession *session,MainWidget* mainWidget, QWidget *parent) :
    QWidget(parent),ui(new Ui::LiveSlaveWidget),mainWidget(mainWidget),session(session)
{
    ui->setupUi(this);

    subLabel=new QLabel(ui->label);
    QRect rect = ui->label->geometry();
    int width=rect.width()*0.2;
    int height=rect.height()*0.2;
    int x=rect.width()-width;
    int y=rect.height()-height;
    subLabel->setGeometry(x,y,width,height);
    subLabel->show();
    //
    QObject::connect(session, &TcpSession::houseMems_signal, this, &LiveSlaveWidget::on_houseMems_signal);
    QObject::connect(session, &TcpSession::houseMsg_signal, this, &LiveSlaveWidget::on_houseMsg_signal);
    QObject::connect(session, &TcpSession::video_signal, this, &LiveSlaveWidget::on_video_signal);
    QObject::connect(session, &TcpSession::camera_signal, this, &LiveSlaveWidget::on_camera_signal);
    QObject::connect(session, &TcpSession::gift_signal, this, &LiveSlaveWidget::on_gift_signal);
    QObject::connect(session, &TcpSession::bullet_signal, this, &LiveSlaveWidget::on_bullet_signal);
    QObject::connect(session, &TcpSession::logout_call_signal, this, &LiveSlaveWidget::on_logout_call_signal);
    //

    //定时器
    timer = new QTimer(this);
    QObject::connect(timer, &QTimer::timeout, this, &LiveSlaveWidget::timeArrived);
    timer->start(100);
}

void LiveSlaveWidget::show(QString & username, QString & nickname, QString & housename, int & houseID)
{
    this->username = username;
    this->nickname = nickname;
    this->housename=housename;
    this->houseID=houseID;
    //界面初始化
    this->setWindowTitle(housename);
    ui->listWidget->clear();
    ui->textBrowser->clear();

    //查询房间的基本信息
    Pack pack;
    pack.setType(HOUSE_ENTER);
    pack.setFrame(CALL);
    pack.append(username);
    pack.append(nickname);
    pack.append(QString::number(houseID));
    pack.append(housename);
    session->send((char*)&pack, pack.getPacksize());

    this->QWidget::show();
}

//界面事件: 退出房间
void LiveSlaveWidget::on_pushButton_exit_clicked()
{
    //发送退出房间的报文
    Pack pack(HOUSE_EXIT, CALL);
    pack.append(username);
    pack.append(nickname);
    pack.append(QString::number(houseID));
    session->send((char*)&pack, pack.getPacksize());
    //
    this->hide();
    mainWidget->show();
}

//发弹幕
void LiveSlaveWidget::on_pushButton_bullet_clicked()
{
    QString msg=ui->lineEdit->text();
    Pack pack(BULLET, CALL);
    pack.append(username);
    pack.append(nickname);
    pack.append(QString::number(houseID));
    pack.append(msg);
    //
    session->send((char*)&pack, pack.getPacksize());
}

//界面事件: 送礼物操作按钮
void LiveSlaveWidget::on_pushButton_gift_clicked()
{
    Pack pack(GIFT, CALL);
    pack.append(username);
    pack.append(nickname);
    pack.append(QString::number(houseID));
    if(ui->radioButton_gift->isChecked()){//送鲜花
        pack.append("flower");
    }else if(ui->radioButton_rocket->isChecked()){//送火箭
        pack.append("rocket");
    }else{
        QMessageBox::critical(this, "错误", "您还没有选择礼物");
        return;
    }
    //
    session->send((char*)&pack, pack.getPacksize());
}

//界面事件: 开/关弹幕
void LiveSlaveWidget::on_pushButton_opt_clicked()
{
    //    关闭弹幕
    QString opt=ui->pushButton_opt->text();
    if(opt=="关闭弹幕"){
        timer->stop();
        QLabel* temp;
        for(QVector<QLabel*>::iterator iter=bullLabels.begin();iter!=bullLabels.end();iter++){
            temp=*iter;
            temp->hide();
            bullLabels.erase(iter);
            delete temp;
            iter--;
        }
        ui->pushButton_opt->setText("打开弹幕");
        return;
    }
    timer->start();
    ui->pushButton_opt->setText("关闭弹幕");
}

//界面槽函数： 界面尺寸调整了
void LiveSlaveWidget::resizeEvent(QResizeEvent *event)
{
    QRect rect = ui->label->geometry();
    int width=rect.width()*0.2;
    int height=rect.height()*0.2;
    int x=rect.width()-width;
    int y=rect.height()-height;

    subLabel->setGeometry(x,y,width,height);
}

//定时器，为弹幕和礼物添加动态效果
void LiveSlaveWidget::timeArrived()
{
    //增加弹幕和礼物的动态效果
    QLabel* temp;
    for(QVector<QLabel*>::iterator iter=bullLabels.begin();iter!=bullLabels.end();iter++){
        QRect rect=(*iter)->geometry();
        (*iter)->move(rect.x()-10, rect.y());
        //
        if(rect.x()+rect.width() < 0){
            temp=*iter;
            bullLabels.erase(iter);
            delete temp;
            iter--;
        }
    }
    //增加礼物的动态效果-火箭
    for(QVector<QLabel*>::iterator iter=rockets.begin();iter!=rockets.end();iter++){
        QRect rect=(*iter)->geometry();
        (*iter)->move(rect.x(), rect.y()-10);
        //
        if(rect.y()+rect.height() < 0){
            temp=*iter;
            rockets.erase(iter);
            delete temp;
            iter--;
        }
    }

}

////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////

//槽函数： 登出信号
void LiveSlaveWidget::on_logout_call_signal(const QString & msg)
{
    QMessageBox::critical(this,"登出提示", msg);
    exit(EXIT_SUCCESS);
}

//槽函数： 收到弹幕信号
void LiveSlaveWidget::on_bullet_signal(const QString &nickname,const QString &houseid, const QString &msg)
{
    int h2=houseid.toInt();
    if(h2 != houseID)
        return;
    QLabel* bull = new QLabel(ui->label);
    bull->setText(msg);
    bull->setStyleSheet("background-color: rgba(255, 255, 255, 0);");
    //x,y
    int width=ui->label->geometry().width();
    int height=ui->label->geometry().height();
    int height2=qrand()%(height/3);
    bull->setGeometry(width, height2, 500,100);
    bull->show();
    bullLabels.append(bull);
}

//槽函数： 房间成员列表信号
void LiveSlaveWidget::on_houseMems_signal(const QStringList &fields)
{
    ui->listWidget->clear();
    for(const QString & str: fields)
        ui->listWidget->addItem(str);
}

//槽函数： 房间消息
void LiveSlaveWidget::on_houseMsg_signal(const QString &msg)
{
    ui->textBrowser->append(msg);
}

//槽函数： 直播视频流
void LiveSlaveWidget::on_video_signal(QImage image)
{
    image = image.scaled(ui->label->size());
    QPixmap pic = QPixmap::fromImage(image);
    ui->label->setPixmap(pic);
}

//槽函数： 直播视频流
void LiveSlaveWidget::on_camera_signal(QImage image)
{
//    image = image.scaled(subLabel->size());
    QPixmap pic = QPixmap::fromImage(image);
    pic=pic.scaled(subLabel->size());
    subLabel->setPixmap(pic);
    subLabel->show();
}

//槽函数： 礼物
void LiveSlaveWidget::on_gift_signal(const QString &nickname, const QString &houseid, const QString &gift)
{
//    if(gift=="flower"){//鲜花效果
    //        qDebug() << "礼物：收到【鲜花礼物】";
    //        return;
    //    }

    if(gift=="rocket"){//火箭效果
        //rocket1.svg
        int r=qrand()%3+1;
        QPixmap pixmap(":/img/rocket"+QString::number(r)+".svg");
        QLabel* rocket = new QLabel(ui->label);
        rocket->setStyleSheet("background-color: rgba(255, 255, 255, 0);");
        int width=ui->label->width();
        int height=ui->label->height();
        rocket->setGeometry(qrand()%width, height, 100, 100);
        pixmap=pixmap.scaled(rocket->size());
        rocket->setPixmap(pixmap);
        rocket->show();
        rockets.append(rocket);
        return;
    }
}

